import os
import re
import sys
from urllib.request import urlopen

from IPython.core.display_functions import clear_output

user_url = "https://casad.cas.cn/ysxx2022/ysmd/dxb/201711/t20171129_4625076.html"
i = 1

dept_pattern = '''<div class="xunhuan">.*?<b>(?P<deptName>.*?)</b>.*?<div class="rmbs_a">(?P<users>.*?)</div>'''
users_pattern = '''<a href="(?P<userMsg_url>.*?)".*?target="_blank">(?P<userName>.*?)</a>'''
userMsg_pattern01 = r'''<p align="justify".*?</p>'''
userMsg_pattern02 = r'''TRS_Editor LI.*?TRS_Editor A.*?</style>.*?</p>'''

reObj_department = re.compile(dept_pattern, re.S)
reObj_users = re.compile(users_pattern, re.S)
reObj_userMsg = re.compile(userMsg_pattern01, re.S)
reObj_userMsg02 = re.compile(userMsg_pattern02, re.S)

# 进入成员信息链接
resp_user = urlopen(user_url)

# *代码补充*
# 提取成员介绍信息
userText_str = str(resp_user.read().decode("utf-8"))

# 从原页面文本中提取图片信息
reObj_img = re.compile('''<div class="acadImg">.*?(?P<img>W.*?.(jpg|png))"''')  # 提取图片网址信息
reObj_pf = re.compile('''^.*/''')  # 提取图片url前缀

img_prefix = reObj_pf.search(user_url).group()
img_main = reObj_img.search(userText_str).group("img")
img = img_prefix + img_main
print(img)

userMsg_search = reObj_userMsg.search(userText_str)
if userMsg_search is None:
    userMsg_search = reObj_userMsg02.search(userText_str)

if userMsg_search is None:
    print("error!")

# 排除多余的非汉字字符
try:
    label_remove = "<.*?>"
    userMsg_text = re.sub(label_remove, '', userMsg_search.group())
except Exception:
    print("提取错误")


print(userMsg_text)
userMsg_text = "地质学家，主要从事青藏高原地质学研究，1965年7月生于安徽省萧县。1988年毕业于北京大学地质学系，1991年于中国科学院地质研究所获硕士学位，1999年于该研究所获博士学位。2017年当选为中国科学院院士。"


# 对提取到的文本进行分析
# 1.提取学术称号
title = ""
re_title01 = '''(?P<title>[\u4e00-\u9fa5]+家)[，。,]'''
re_title02 = '''(?P<title>[\u4e00-\u9fa5]+学)[，。,]'''
for re_title in [re_title01, re_title02]:
    reObj_title = re.compile(re_title)
    title_re = reObj_title.search(userMsg_text)
    try:
        title = title_re.group("title")
        break
    except Exception:
        continue
else:
    title = "提取学术称号失败!\n\n"


# 4.提取专业领域
re_short_text = '''(学|家)[。，,](?P<short>.*)[。，].*?(出)?生(于)?'''
reObj_short_text = re.compile(re_short_text)
try:
    short_text = reObj_short_text.search(userMsg_text).group("short")
except AttributeError:
    short_text = "提取信息出错!"

f_remove = open("E:\idea+\Python_experiment\Crawler\去除词.txt", mode="r", encoding="utf-8")
remove = f_remove.read()
f_remove.close()

remove = remove.replace('\n', '?')
area = re.sub(remove, '', short_text)

# 2.提取出生年月及出生地

birth_re1 = '''[。](?P<birthday>[\d年月]+).*?生于(?P<birthplace>.*?)[，。,.]'''
birth_re2 = '''(?P<birthday>[\d年月]+)(出)?生[，。,.](?P<birthplace>.*?)人[。，.,]'''
birth_re3 = '''[，](?P<birthday>[\d年月]+).*?生于(?P<birthplace>.*?)[，。,.]'''
birth_re4 = '''[，。,.](?P<birthday>[\d年月日]+)出生'''
birthday = ""
birthplace = ""

for birth_re in [birth_re1, birth_re2, birth_re3]:
    reObj_birth = re.compile(birth_re)

    birth = reObj_birth.search(userMsg_text)
    if birth is None:
        continue
    else:
        birthday = birth.group("birthday")
        birthplace = birth.group("birthplace")
        break
else:
    reObj_no_place = re.compile(birth_re4)
    birthday = reObj_no_place.search(userMsg_text).group("birthday")
    birthplace = "暂无信息"

# 3.提取当选年份
date_re = '''[，。,.](?P<date>\d+年?)当选'''
date = ""
reObj_date = re.compile(date_re)
try:
    date = reObj_date.search(userMsg_text).group("date")
except AttributeError:
    date = "提取当选年份失败!\n\n"



os.system("Clear-Host")
print("学术称号:", title)
print("出生年月:", birthday)
print("出生地:", birthplace)
print("当选年份:", date)
print("专业领域:", area)
